package com.itheima.health.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.common.PageParam;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.mapper.CheckItemDao;
import com.itheima.health.mapper.CheckgroupCheckitemDao;
import com.itheima.health.mapper.CheckgroupDao;
import com.itheima.health.model.pojos.CheckGroup;
import com.itheima.health.model.pojos.CheckgroupCheckitem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckgroupService {
    /**
     * 检查组查询
     *
     * @param pageParam
     * @return
     */
    @Autowired
    CheckgroupDao checkgroupDao;
    @Autowired
    CheckItemDao itemDao;
    @Autowired
    CheckgroupCheckitemDao checkgroupCheckitemDao;

    public ResultPageData cGPpage(PageParam pageParam) {
        Integer currentPage = pageParam.getCurrentPage();
        Integer pageSize = pageParam.getPageSize();
        IPage<CheckGroup> page = new Page<>(currentPage, pageSize);

        LambdaQueryWrapper<CheckGroup> query = new LambdaQueryWrapper<>();

        IPage<CheckGroup> pageData = checkgroupDao.selectPage(page, query);
        long total = pageData.getTotal();
        List<CheckGroup> records = pageData.getRecords();

        ResultPageData resultPageData = new ResultPageData();
        resultPageData.setTotal(total);
        resultPageData.setRows(records);
        return resultPageData;
    }


    /**
     * 根据id回显检查组
     *
     * @param id
     * @return
     */
    public CheckGroup checkgroupById(Long id) {
        return checkgroupDao.selectById(id);
    }

    /**
     * 检查组新增 包含检查项
     */
    public void add(CheckGroup checkGroup,Integer[]checkitemIds) {
        checkgroupDao.insert(checkGroup);
        for (Integer checkitemId : checkitemIds) {
            CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem();
            checkgroupCheckitem.setCheckgroupId(checkGroup.getId());
            checkgroupCheckitem.setCheckitemId(checkitemId);
            checkgroupCheckitemDao.insert(checkgroupCheckitem);
        }
    }


}